<?php

namespace StudyBuddy\Api\v1;

use StudyBuddy\Api\Api;

/**
 * API Controller to get
 * all answers to a particular question
 * It may omit actual question
 * if justanswers=yes is passed in request
 *
 */
class Question extends Questions {

    /**
     * 'qid' is a require request param for this controller
     *
     * @var array
     */
    protected $aRequired = array('qid');

    /**
     * Question id
     *
     * @var int
     */
    protected $qid;
    protected $aQuestion = array();

    /**
     * Array of answers for a question
     * this may not include all the answers but
     * only the ones that fit withing this one "page"
     * in case where there are more answers
     * than a per-page value
     *
     *
     * @var array
     */
    protected $aAnswers = array();

    /**
     * Allowed values of the 'sort' param
     *
     * @var array
     */
    protected $allowedSortBy = array('i_lm_ts', '_id', 'i_votes', 'accepted');
    protected $aFields = array(
        'a_title' => 0,
        'i_views' => 0,
        'i_favs' => 0,
        'i_flwrs' => 0,
        'i_sel_uid' => 0,
        'i_qid' => 0,
        'i_quid' => 0,
        'tags_html' => 0,
        'credits' => 0,
        'ans_s' => 0,
        'v_s' => 0,
        'f_s' => 0,
        'a_uids' => 0,
        'a_flwrs' => 0,
        'sim_q' => 0,
        'vw_s' => 0,
        'a_comments' => 0,
        'a_latest' => 0,
        'a_deleted' => 0);

    protected function main() {
        $this->pageID = $this->oRequest['pageID'];
        $this->qid = $this->oRequest['qid'];

        if (true === $this->oRequest->get('comments', 'b')) {
            unset($this->aFields['a_comments']);
        }

        $this->setQuestion()
                ->setSortBy()
                ->setSortOrder()
                ->setStartId()
                ->setStartTime()
                ->setLimit()
                ->getCursor()
                ->setOutput();
    }

    /**
     * Get question data
     *
     * @return object $this
     */
    protected function setQuestion() {
        $this->aQuestion = $this->oRegistry->Mongo->QUESTIONS->findOne(array('_id' => $this->qid), $this->aFields);

        if (empty($this->aQuestion)) {
            throw new \StudyBuddy\HttpResponseCodeException('Question with id: ' . $this->qid . ' not found', 404);
        }

        if (!empty($this->aQuestion['i_del_ts'])) {
            throw new \StudyBuddy\HttpResponseCodeException('Question with id: ' . $this->qid . ' has been deleted on ' . date('r', $this->aQuestion['i_del_ts']), 410);
        }

        return $this;
    }

    /**
     * In this class getCursor has to do with
     * getting answers for one question
     *
     * @return object $this
     */
    protected function getCursor() {
        /**
         * Add 'title' to excluded fields
         * because title in "Answers" always the same as in 
         * Question
         * 
         */
        $this->aFields['title'] = 0;

        $where = array(
            'i_del_ts' => null,
            'i_qid' => $this->oRequest['qid']);

        if ($this->startTime) {
            $where['i_lm_ts'] = array('$gt' => (int) $this->startTime);
        }

        if ($this->startId) {
            $where['_id'] = array('$gt' => (int) $this->startId);
        }


        if ($this->maxId) {
            $where['_id'] = array('$lt' => (int) $this->maxId);
        }

        d('$where: ' . print_r($where, 1));


        $sort[$this->sortBy] = $this->sortOrder;

        $offset = (($this->pageID - 1) * $this->limit);
        d('offset: ' . $offset);

        $this->cursor = $this->oRegistry->Mongo->ANSWERS->find($where, $this->aFields)
                ->sort($sort)
                ->limit($this->limit)
                ->skip($offset);

        $this->count = $this->cursor->count();
        d('count: ' . $this->count);

        if (0 < $this->count) {


            $this->aAnswers = array(
                'total' => $this->count,
                'page' => $this->pageID,
                'perpage' => $this->limit,
                'answers' => \iterator_to_array($this->cursor, false)
            );

            $this->aQuestion['answers'] = $this->aAnswers;
        }

        return $this;
    }

    protected function setOutput() {


        $this->oOutput->setData($this->aQuestion);

        return $this;
    }

}
